/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmi;

import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author thomas
 */
public class Annuaire extends UnicastRemoteObject implements AnnuaireInterface {

    private HashMap<String,Numero> abonnes = new HashMap();
    
    public Annuaire(String adresseFileHotel) throws ParserConfigurationException, SAXException, IOException {
        /* Récupération de l'annuaire dans le fichier xml */
        DocumentBuilder docBuilder = null;
        Document doc = null;
        docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = docBuilder.parse(new File(adresseFileHotel));

        String name, numero;
        NodeList list = doc.getElementsByTagName("Telephone");
        NamedNodeMap attrs;

        for (int i = 0; i < list.getLength(); i++) {
            attrs = list.item(i).getAttributes();
            name = attrs.getNamedItem("name").getNodeValue();
            numero = attrs.getNamedItem("numero").getNodeValue();
            Numero num = new Numero(numero);
            abonnes.put(name, num);
        }
    }

    public Numero getNumero(String abonne) throws RemoteException {
        return abonnes.get(abonne);
    }
}
